package com.example.h1909aapp;

import android.widget.FrameLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.h1909aapp.base.BaseActivity;
import com.example.h1909aapp.fragment.DiscoveryFragment;
import com.example.h1909aapp.presenter.MainPresenter;
import com.example.h1909aapp.view.MainView;
import com.google.android.material.tabs.TabLayout;
import com.hyphenate.easeui.ui.EaseContactListFragment;
import com.hyphenate.easeui.ui.EaseConversationListFragment;

import java.util.ArrayList;

import butterknife.BindView;


//友盟AppKey：5f0faf59978eea08cad0c8ad
//继承BaseActivity的时候提供了泛型,这个是这个Activity对应的P层类型
public class MainActivity extends BaseActivity<MainPresenter>
        implements MainView {


    @BindView(R.id.fl)
    FrameLayout mFl;
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    private ArrayList<Fragment> mFragments;
    private FragmentManager mManager;
    //要隐藏fragmnet的position
    private int mLastFragmentPosition = 0;

    //就近原则
    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected MainPresenter initPresenter() {
        //MainPresenter presenter = new MainPresenter();
        //不通过构造传递view层对象,通过方法传递,
        //因为所有p层创建的时候都需要持有view
        //presenter.bindView(this);
        return new MainPresenter();
    }

    @Override
    protected void initData() {
        mPresenter.getData();
    }

    @Override
    protected void initView() {
        mManager = getSupportFragmentManager();
        initFragment();
        initTab();

        showConversionFragment();

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switchFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void showConversionFragment() {
        mManager.beginTransaction()
                .add(R.id.fl,mFragments.get(0))
                .commit();
    }

    private void switchFragment(int position) {
        FragmentTransaction transaction = mManager.beginTransaction();
        Fragment showFragment = mFragments.get(position);
        Fragment hideFragment = mFragments.get(mLastFragmentPosition);

        //多次添加会崩
        if (!showFragment.isAdded()){
            transaction.add(R.id.fl,showFragment);
        }
        transaction.show(showFragment);
        transaction.hide(hideFragment);
        transaction.commit();
        //当前显示的fragment，下次点击tab就是要隐藏的fragmnet
        mLastFragmentPosition = position;
    }

    private void initTab() {

       mTabLayout.addTab(mTabLayout.newTab().setText("会话").setIcon(R.drawable.selector_conversion));
       mTabLayout.addTab(mTabLayout.newTab().setText("联系人").setIcon(R.drawable.selector_contact));
       mTabLayout.addTab(mTabLayout.newTab().setText("发现").setIcon(R.drawable.selector_discovery));
       //mTabLayout.addTab(mTabLayout.newTab().setCustomView());
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        //会话fragment
        EaseConversationListFragment conversationListFragment = new EaseConversationListFragment();
        //联系人fragment
        EaseContactListFragment contactListFragment = new EaseContactListFragment();
        mFragments.add(conversationListFragment);
        mFragments.add(contactListFragment);

        mFragments.add(DiscoveryFragment.getInstance());
    }


}
